
/**
 * 通过递归过滤异步路由表
 * @param routes asyncRoutes
 * @param roles
 */
import { RouteRecordRaw } from 'vue-router'
export function filterAsyncRoutes(routes:Array< RouteRecordRaw>, roles:Array<string>) {
  const router:RouteRecordRaw[] = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      router.push(tmp)
    }
  })
  return router
}

/**
 * 使用 meta.role 来确定当前用户是否具有权限
 * @param roles
 * @param route
 */
export function hasPermission(roles:Array<string>, route:RouteRecordRaw) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => {
      if (route.meta?.roles) {
        return (route.meta.roles as Array<string>).includes(role)
      }
    })
  } else {
    return false
  }
}

/**
 * @description 使用递归，过滤需要缓存的路由
 * @param {Array} _route 所有路由表
 * @param {Array} _cache 缓存的路由表
 * @return void
 * */

export function filterKeepAlive(routers:Array< RouteRecordRaw>) {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const cacheRouter: any[] = []
  const deep = (routers:Array< RouteRecordRaw>) => {
    routers.forEach(item => {
      if (item.meta?.keepAlive && item.name) {
        cacheRouter.push(item.name)
      }
      if (item.children && item.children.length) {
        deep(item.children)
      }
    })
  }
  deep(routers)
  return cacheRouter
}
